<script>
import outMain from "@/components/outMain.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  AccreditationAdd,
  AccreditationEdit,
  AccreditationDetail,
} from "@/http/api";
export default {
  components: { outMain },
  name: "",
  data() {
    const checkNumber = (rule, value, callback) => {
      let result = this.$ruleCheck.checkNumber(value);
      if (!result) {
        callback();
      } else {
        callback(new Error(result));
      }
    };
    return {
      detailId: this.$route.query.id,
      showCont: false,
      formData: {
        project_name: "",
        project_price: "",
        expiration_time: "",
      },
      rules: {
        project_name: [
          {
            required: true,
            message: "请输入证件名称",
            trigger: "blue",
          },
        ],
        project_price: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur",
          },
        ],
      },
    };
  },
  props: {},
  setup() {},
  methods: {
    backFn() {
      this.$router.back();
    },
    async saveFn() {
      const valid = await this.$refs.ruleForm.validate();
      if (!valid) {
        return;
      }
      const { data } = this.detailId
        ? await AccreditationEdit(this.formData)
        : await AccreditationAdd(this.formData);
      if (data.code == 1) {
        ElMessage({
          type: "success",
          message: this.detailId ? "编辑成功" : "新增成功",
        });
        setTimeout(() => {
          this.$router.back();
        }, 1000);
      }
    },
    getData() {
      AccreditationDetail({ project_id: this.detailId }).then((res) => {
        if (res.data.code == 1) {
          let resData = res.data.data;
          this.formData = res.data.data;
          this.formData["project_id"] = this.detailId;
          this.showCont = true;
        }
      });
    },
  },
  mounted() {},
  created() {
    if (this.detailId) {
      this.getData();
    } else {
      this.showCont = true;
    }
  },
};
</script>
<template>
  <outMain>
    <div class="formCont">
      <div class="formHead">
        <div>新增/编辑共享</div>
        <btn value="返回上一级" @ok="backFn"></btn>
      </div>
      <div class="formMain">
        <el-form :rules="rules" ref="ruleForm" :model="formData">
          <div class="flexCont" v-if="showCont">
            <el-form-item
              label="产品名称"
              class="flexPull"
              label-width="120"
              prop="project_name"
            >
              <el-input
                clearable
                placeholder="请输入产品名称"
                v-model="formData.project_name"
              />
            </el-form-item>
            <el-form-item
              label="共享价格"
              class="flexPull"
              label-width="120"
              prop="project_price"
            >
              <el-input
                clearable
                placeholder="请输入共享价格"
                v-model="formData.project_price"
                ><template #append>元/年</template></el-input
              >
            </el-form-item>
            <el-form-item
              label="到期时间"
              class="flexPull"
              label-width="120"
              prop="expiration_time"
            >
              <el-date-picker
                v-model="formData.expiration_time"
                type="day"
                style="width: 100%"
                clearable
                placeholder="请选择到期时间"
              />
            </el-form-item>
            <el-form-item class="flexPull" label=" " label-width="120">
              <btn value="确认保存" @ok="saveFn"></btn>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </div>
  </outMain>
</template>
<style scoped lang="less"></style>
